package com.riven.bilibilidemo.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextPaint;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.riven.bilibilidemo.R;
import com.riven.bilibilidemo.adapter.RegionPagerAdapter;
import com.riven.bilibilidemo.bean.RegionTypesInfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;

public class RegionTypeDetailsActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.sliding_tabs)
    SlidingTabLayout slidingTabs;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    private RegionTypesInfo.DataBean mDataBean;
    private List<String> titles = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_region_type_details);
        ButterKnife.bind(this);
        Bundle mBundle = getIntent().getExtras();
        if (mBundle != null) {
            mDataBean = mBundle.getParcelable("zzz");
        }
        initViewPager();

    }

    private void initViewPager() {
        titles.add("推荐");
        Observable.from(mDataBean.getChildren())
                .subscribe(childrenBean -> {
                    titles.add(childrenBean.getName());
                });
        RegionPagerAdapter mAdapter = new RegionPagerAdapter(
                getSupportFragmentManager(), mDataBean.getTid(), titles, mDataBean.getChildren());
        viewPager.setOffscreenPageLimit(titles.size());
        viewPager.setAdapter(mAdapter);
        slidingTabs.setViewPager(viewPager);
        //动态设置tabView的下划线宽度
        measureTabLayoutTextWidth(0);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }


            @Override
            public void onPageSelected(int position) {

                measureTabLayoutTextWidth(position);
            }


            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    public void measureTabLayoutTextWidth(int position) {

        String titleName = titles.get(position);
        TextView titleView = slidingTabs.getTitleView(position);
        TextPaint paint = titleView.getPaint();
        float v = paint.measureText(titleName);
        slidingTabs.setIndicatorWidth(v / 3);
    }

    public static void launch(Activity activity, RegionTypesInfo.DataBean dataBean) {

        Intent mIntent = new Intent(activity, RegionTypeDetailsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putParcelable("zzz", dataBean);
        mIntent.putExtras(bundle);
        activity.startActivity(mIntent);
    }
}
